class Observer {
  constructor (data) {
    this.walk(data)
  }
  walk (data) {
    // 1、判断是否为对象
    if (!data || typeof data !== 'object') return
    // 2、遍历 data 属性
    Object.keys(data).forEach(key => {
      this.defineReactive(data, key, data[key])
    })
  }
  defineReactive (data, key, val) {
    const _this = this
    
    const dep = new Dep()
    this.walk(val)
    Object.defineProperty(data, key, {
      enumerable: true,
      configurable: true,
      get () {
        // 收集依赖
        Dep.target && dep.addsubs(Dep.target)
        return val
      },
      set (newVal) {
        if (newVal === val) return
        val = newVal
        _this.walk(val)
        // 发送通知
        dep.notify()
      }
    })
  }
}